// pages/companyRegister/companyRegister.js
const app = getApp();


Page({
  /**
   * 页面的初始数据
   */
  data: {
    /**
     * 行业
     */
    industrys: ["互联网", "教育", "医疗", "金融"],
    industryIndex: 0,

    modify: false,
    userInfo: null,
    websiteArr: {
      company_website_1: null,
      company_website_2: null,
      company_website_3: null,
    },
    /**
     * tips
     */
    tipsInfo: {
      tipsType: null,
      showTopTips: false,
      errorMsg: null,
    }
  },
  /**
   * 行业下拉列表
   */

  bindIndustryChange: function (e) {
    console.log('picker industry 发生选择改变，携带值为', e.detail.value);

    this.setData({
      industryIndex: e.detail.value
    })
  },
  /**
   * tips提示 
   */
  showTopTips: function (msg, Type) {
    var that = this;

    that.setData({
      'tipsInfo.showTopTips': true,
      'tipsInfo.errorMsg': msg,
      'tipsInfo.tipsType': Type
    });
    setTimeout(function () {
      that.setData({
        'tipsInfo.showTopTips': false
      });
    }, 3000);
  },
  /**
   * submit
   */
  formSubmit: function (e) {
    var that = this;

    var values = e.detail.value;
    console.log(values);

    var website_1 = values.company_website_1;
    var website_2 = values.company_website_2;
    var website_3 = values.company_website_3;
    var websites;
    var webArr = [];

    if (website_1 != "") {
      webArr.push(website_1);
    }
    if (website_2 != "") {
      webArr.push(website_2);
    }
    if (website_3 != "") {
      webArr.push(website_3);
    }
    console.log("webArrayLength:" + webArr.length);


    if (webArr.length > 0) {
      websites = '外链1|' + webArr[0];
    }
    if (webArr.length > 1) {
      websites = websites + '|外链2|' + webArr[1];
    }
    if (webArr.length > 2) {
      websites = websites + '|外链3|' + webArr[2];
    }

    if (values.company_intro == "" || webArr.length==0) {
      that.showTopTips('请填写完整', 'warn');
      return;
    }

    // 注册
    if (!that.data.modify) {
      console.log(values);

      if (values.company_name == "" ||
        //values.company_industry == null ||
        values.company_city == "" ||
        values.company_business_content == "" ||
        values.company_location == ""
      ) {
        that.showTopTips('请填写完整', 'warn');
        return;
      }
      var tempData;
      tempData = {
        'company_name': e.detail.value.company_name,
        'company_industry': that.data.industrys[that.data.industryIndex],
        'company_city': e.detail.value.company_city,
        'company_business_content': e.detail.value.company_business_content,
        'company_stock_code': e.detail.value.company_stock_code,
        'company_location': e.detail.value.company_location,
        'company_website': websites,
        'company_intro': e.detail.value.company_intro,
      };

      console.log(tempData);

      wx.request({
        url: app.globalData.server + '/user/company/auth/',
        method: 'POST',
        data: tempData,
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
          'Authorization': 'Bearer ' + app.globalData.token,
          'x-wxapp-signature': null,
          'x-wxapp-timestamp': app.getTimestamp(),
          'x-wxapp-nonce': app.getRandom()
        },
        success: function (res) {
          // 根据res反馈结果
          var msg = res.data.message;
          console.log(res);
          if (msg == "ok") {
            that.showTopTips('注册成功，等待审核', 'success');
            // 重新进入程序入口
            setTimeout(function () {
              wx.reLaunch({
                url: '../init/init',
              })
            }, 1000);
          } else {
            that.showTopTips('注册失败', 'warn');
          }
        }
      })
      /**
       * 修改企业信息
       */
    } else {
      
      wx.request({
        url: app.globalData.server + '/user/',
        method: 'POST',
        data: {
          'company_website': websites,
          'company_intro': e.detail.value.company_intro,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
          'Authorization': 'Bearer ' + app.globalData.token,
          'x-wxapp-signature': null,
          'x-wxapp-timestamp': app.getTimestamp(),
          'x-wxapp-nonce': app.getRandom()
        },
        success: function (res) {
          // 根据res反馈结果
          var msg = res.data.message;
          console.log(res);
          // old_passwd错误
          if (msg == "ok") {
            that.showTopTips('修改成功', 'success');
            // 密码修改成功
          } else {
            that.showTopTips('修改失败', 'warn');
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    // 修改信息
    if (options.modify && app.globalData.userType == 3) {

      // 更新userInfo
      wx.request({
        url: app.globalData.server + '/user/',
        method: 'GET',
        data: {},
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
          'Authorization': 'Bearer ' + app.globalData.token,
          'x-wxapp-signature': null,
          'x-wxapp-timestamp': app.getTimestamp(),
          'x-wxapp-nonce': app.getRandom()
        },
        success: function (res) {
          app.globalData.userInfo = res.data.data;


          // 初始化表单
          that.setData({
            modify: true,
            userInfo: app.globalData.userInfo,
          });
          var website = that.data.userInfo.company_website;

          var websiteArr = website.split("|");
          //var length = websiteArr.length();
          console.log(websiteArr);
          that.setData({
            'websiteArr.company_website_1': websiteArr[1],
            'websiteArr.company_website_2': websiteArr[3],
            'websiteArr.company_website_3': websiteArr[5],
          });
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})